package com.dmooo.ymx.mall;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.dmooo.ymx.R;
import com.dmooo.ymx.base.BaseActivity;
import com.dmooo.ymx.bean.Response;
import com.dmooo.ymx.config.Constants;
import com.dmooo.ymx.https.HttpUtils;
import com.dmooo.ymx.https.onOKJsonHttpResponseHandler;
import com.dmooo.ymx.malladapter.ExpressDetailAdapter;
import com.dmooo.ymx.mallbean.ExpressDetailBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cz.msebera.android.httpclient.Header;

/**
 * 物流详情
 */
public class ExpressActivity extends BaseActivity {
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recy_wuliu)
    RecyclerView recyWuliu;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    private List<ExpressDetailBean> list = new ArrayList<>();
    private ExpressDetailAdapter adapter;

    @Override
    protected void initUI() {
        setContentView(R.layout.activity_wuliu);
        ButterKnife.bind(this);
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("物流详情");
    }

    @Override
    protected void initData() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyWuliu.setLayoutManager(linearLayoutManager);
        adapter = new ExpressDetailAdapter(R.layout.item_express_detail, list);
        recyWuliu.setAdapter(adapter);
    }

    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getExpressDetail();
            }
        });
        refreshLayout.autoRefresh();
        refreshLayout.setEnableLoadMore(false);
    }

    private void getExpressDetail() {
        list.clear();
        RequestParams requestParams = new RequestParams();
        requestParams.put("logistics", getIntent().getStringExtra("logistics"));
        requestParams.put("express_number", getIntent().getStringExtra("number"));
        HttpUtils.post("1".equals(Constants.MALL_ORDER_TYPE) ? Constants.EXPRESS_DETAIL_UPDATE : Constants.EXPRESS_DETAIL, requestParams, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    JSONObject o = new JSONObject(responseString);
                    if (o.getInt("code") == 0) {
                        list.clear();
                        JSONArray array = o.getJSONObject("data").getJSONArray("logisticsMsg");
                        for (int i = 0; i < array.length(); i++) {
                            list.add(new Gson().fromJson(array.getJSONObject(i).toString(), ExpressDetailBean.class));
                        }
                    } else {
                        showToast(o.getString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        refreshLayout.finishRefresh();
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }

    @OnClick(R.id.tv_left)
    public void onViewClicked() {
        finish();
    }
}
